package com.yidu.logisticssystem.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsPost;
import com.yidu.logisticssystem.service.LogisticsPostService;
import com.yidu.logisticssystem.utils.BaseMessage;
import com.yidu.logisticssystem.utils.Tools;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 职位(LogisticsPost)表控制层
 *
 * @author 王锦程
 * @since 2021-03-08 16:46:52
 */
@RestController
@RequestMapping("logisticsPost")
public class LogisticsPostController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsPostService logisticsPostService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsPost selectOne(Integer id) {
        return this.logisticsPostService.queryById(id);
    }


    /**
     * 通过主键删除单条数据
     *
     * @param postId 主键
     * @return 单条数据
     */
    @GetMapping("delete")
    public Map<String, Object> delete(Integer postId) {
        Map<String, Object> map = new HashMap<>();
        boolean b = logisticsPostService.deleteById(postId);
        if (b==true){
            map.put("mes","删除成功");
        }else {
            map.put("mes","删除失败");
        }
        return map;
    }


    /**
     * 查询所有数据
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value ="查询所有角色" )
    @GetMapping("selectAll")
    public Map<String,Object> selectAll(int page,int limit) {
        //调用PageHelper.startPage将参数加入到分页助手
        PageHelper.startPage(page, limit);
        Map<String,Object> map = new HashMap<>();
        //此处应该调用queryAllByLimit查询角色
        List<LogisticsPost> list = logisticsPostService.queryAllByLimit();
        //使用PageInfo<LogisticsPost>泛型
        PageInfo<LogisticsPost> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data", pageInfo.getList());//所有数据pageInfo.getList()
        map.put("count",pageInfo.getTotal());//总行数pageInfo.getTotal()
        return map;
    }

    /**
     * 根据id查询角色数据
     * @return
     */
    @ApiOperation(value ="根据id查询角色数据" )
    @PostMapping("selectId")
    public Map<String,Object> selectAll(Integer postId) {
        Map<String,Object> map = new HashMap<>();
        //调用queryById查询
        LogisticsPost logisticsPost = logisticsPostService.queryById(postId);
        map.put("data",logisticsPost);
        return map;
    }

    /**
     * 查询职位ID和职位名称
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询职位ID和职位名称")
    public Map<String,Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        //调用queryAllByLimit
        List<LogisticsPost>list=logisticsPostService.queryAllByLimit();
        map.put("data",list);
        return map;
    }

    /**
     * 查询职位
     * @param sectionId
     * @return
     */
    @PostMapping("select")
    @ApiOperation("查询职位")
    public Map<String,Object> select(Integer sectionId){
        //调用select查询
        List<LogisticsPost> list=logisticsPostService.select(sectionId);
        //使用工具类getLayUiMap返回数据
        return Tools.getLayUiMap(list);
    }
}
